﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace PhotoViewer
{
    public partial class frmSpecialEfficacy : Form
    {
        public frmSpecialEfficacy()
        {
            InitializeComponent();
        }

        public Image image;

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSpecialEfficacy_Load(object sender, EventArgs e)
        {
            this.ptbOld.Image = image;
            this.ptbNew.Image = image;
        }

        /// <summary>
        /// combox改变索引值触发该事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPicEffect_SelectedIndexChanged(object sender, EventArgs e)
        {
            ptbNew.Image = image;
            string picType = this.cmbPicEffect.Text.Trim();//选择特效类型
            switch (picType)
            {
                case "Relief":
                    ReliefEffect();//浮雕
                    break;
                case "Building Block":
                    BuildingBlockEffect();//积木
                    break;
                case "Negative":
                    NegativeEffect();//底片
                    break;
                case "Black-and-White":
                    BlackAndWhiteEffect();//黑白
                    break;
                case "Oilpaint":
                    OilPaint();//油画
                    break;
            }

        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbPicEffect.Text.Trim() == "")
            {
                MessageBox.Show("Please choose a efficacy", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {

                saveFileDialog1.Filter = "BMP|*bmp|JPEG|*jpeg|GIF|*gif|PNG|*png";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string picPath = saveFileDialog1.FileName;
                    string picType = picPath.Substring(picPath.LastIndexOf(".") + 1, picPath.Length - picPath.LastIndexOf(".") - 1);
                    picType.ToLower();

                    switch (picType)
                    {
                        case "bmp":
                            Bitmap bitmap1 = new Bitmap(ptbNew.Image);//获取处理后的图片
                            Bitmap myBitmap = new Bitmap(bitmap1, image.Width, image.Height);//得到一张与原先大小一样的图片
                            myBitmap.Save(picPath, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;
                        case "jpeg":
                            Bitmap bitmap2 = new Bitmap(ptbNew.Image);
                            Bitmap myBitmap2 = new Bitmap(bitmap2, image.Width, image.Height);
                            myBitmap2.Save(picPath, ImageFormat.Jpeg);
                            break;
                        case "gif":
                            Bitmap bitmap3 = new Bitmap(ptbNew.Image);
                            Bitmap myBitmap3 = new Bitmap(bitmap3, image.Width, image.Height);
                            myBitmap3.Save(picPath, ImageFormat.Gif);
                            break;
                        case "png":
                            Bitmap bitmap4 = new Bitmap(ptbNew.Image);
                            Bitmap myBitmap4 = new Bitmap(bitmap4, image.Width, image.Height);
                            myBitmap4.Save(picPath, ImageFormat.Png);
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to close the window?", "Prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                this.Close();
            }
        }

        #region 自定义方法

        /// <summary>
        /// 浮雕处理
        /// </summary>
        public void ReliefEffect()
        {
            try
            {
                Bitmap myBitmap = new Bitmap(ptbOld.Image);
                for (int i = 0; i < myBitmap.Width - 1; i++)
                {
                    for (int j = 0; j < myBitmap.Height - 1; j++)
                    {
                        Color Color1 = myBitmap.GetPixel(i, j);
                        Color Color2 = myBitmap.GetPixel(i + 1, j + 1);

                        int red = Math.Abs(Color1.R - Color2.R + 128);
                        int green = Math.Abs(Color1.G - Color2.G + 128);
                        int blue = Math.Abs(Color1.B - Color2.B + 128);
                        //颜色处理
                        if (red > 255) red = 255;
                        if (red < 0) red = 0;

                        if (green > 255) green = 255;
                        if (green < 0) green = 0;

                        if (blue > 255) blue = 255;
                        if (blue < 0) blue = 0;
                        myBitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));
                    }
                }
                ptbNew.Image = myBitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 积木效果
        /// </summary>
        public void BuildingBlockEffect()
        {
            Graphics graphic = this.CreateGraphics(); //创建窗体的graphics对象
            Bitmap myBitmap = new Bitmap(ptbNew.Image);

            Color color;
            Color newColor;

            int imageWidth = myBitmap.Width;
            int imageHeight = myBitmap.Height;

            int pixelAvg;
            int pixelValue;

            RectangleF rect = new RectangleF(0, 0, imageWidth, imageHeight);//设置绘制图片区域
            Bitmap bitmap = myBitmap.Clone(rect, PixelFormat.DontCare);//在指定的区域创建bitmap对象

            for (int x = 0; x < imageWidth - 1; x++)
            {
                for (int y = 0; y < imageHeight - 1; y++)
                {
                    pixelValue = 0;//设置默认值
                    color = bitmap.GetPixel(x, y);//获取该像素点的像素值
                    pixelAvg = (color.R + color.G + color.B) / 3;
                    if (pixelAvg >= 128)
                        pixelValue = 255;//如果平均值大雨128则将其设置成白色
                    else
                        pixelValue = 0;

                    //将重新设置的值设置成新颜色值
                    newColor = Color.FromArgb(255, pixelValue, pixelValue, pixelValue);
                    //重新设置该像素点的值
                    bitmap.SetPixel(x, y, newColor);
                }//y
            }//x

            graphic.Clear(Color.WhiteSmoke);//用该颜色清楚graphics对象
            graphic.DrawImage(bitmap, new Rectangle(0, 0, imageWidth, imageHeight));
            ptbNew.Image = bitmap;
        }

        /// <summary>
        /// 底片效果
        /// </summary>
        public void NegativeEffect()
        {
            int width = this.ptbOld.Image.Width;
            int height = this.ptbOld.Image.Height;
            Color color;
            Color newColor;

            Bitmap newBitmap = new Bitmap(width, height);//穿件bitmap对象
            Bitmap oldBitmap = (Bitmap)this.ptbOld.Image;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int r;
                    int g;
                    int b;
                    color = oldBitmap.GetPixel(x, y);
                    //颜色取反
                    r = 255 - color.R;
                    g = 255 - color.G;
                    b = 255 - color.B;
                    newColor = Color.FromArgb(255, r, g, b);
                    newBitmap.SetPixel(x, y, newColor);
                }//y
            }//x

            this.ptbNew.Image = newBitmap;
        }

        /// <summary>
        /// 黑白效果（将每个像素点的值设置成为每个像素平均值）
        /// </summary>
        public void BlackAndWhiteEffect()
        {
            int width = this.ptbOld.Image.Width;
            int height = this.ptbOld.Image.Height;
            Color color;
            Color newColor;

            //根据图像大小创建bitmap对象
            Bitmap newBitmap = new Bitmap(width, height);
            Bitmap oldBitmap = new Bitmap(this.ptbOld.Image);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    color = oldBitmap.GetPixel(x, y);
                    int r;
                    int g;
                    int b;
                    r = color.R;
                    g = color.G;
                    b = color.B;
                    int pixelAvg = (r + g + b) / 3;

                    newColor = Color.FromArgb(255, pixelAvg, pixelAvg, pixelAvg);
                    newBitmap.SetPixel(x, y, newColor);
                }//y
            }//x
            this.ptbNew.Image = newBitmap;
        }

        /// <summary>
        /// 油画效果
        /// </summary>
        public void OilPaint()
        {
            Graphics graphics = this.ptbNew.CreateGraphics();
            Bitmap bitmap = new Bitmap(ptbOld.Image);
            int width = bitmap.Width;
            int height = bitmap.Height;

            RectangleF rect = new RectangleF(0, 0, width, height);
            //定义一个bitmap副本
            Bitmap newBitmap = bitmap.Clone(rect, PixelFormat.DontCare);

            Random random = new Random();

            //设置不同的值来决定油画的效果的不同程度
            int effect = 3;
            for (int x = width - effect; x > 1; x--)
            {
                for (int y = height - effect; y > 1; y--)
                {
                    int newPos = random.Next(100000) % effect;
                    //将该点的RGB的值设置成附近effect点之内的任意一点的rgb的值
                    Color color = newBitmap.GetPixel(newPos + x, newPos + y);
                    newBitmap.SetPixel(x, y, color);
                }//y
            }//x

            graphics.Clear(Color.WhiteSmoke);
            graphics.DrawImage(newBitmap, new Rectangle(0, 0, width, height));
            this.ptbNew.Image = newBitmap;
        }

        #endregion
    }
}
